package com.xiangban.yanglaodajie_admin.service;

import com.xiangban.yanglaodajie_admin.dao.GoodsMapper;
import com.xiangban.yanglaodajie_admin.dao.OrdersMapper;
import com.xiangban.yanglaodajie_admin.entity.Orders;
import com.xiangban.yanglaodajie_admin.entity.OrdersData;
import com.xiangban.yanglaodajie_admin.entity.dto.OrdersDto;
import com.xiangban.yanglaodajie_admin.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_admin.exception.BizException;
import com.xiangban.yanglaodajie_admin.utils.CommonUtils;
import com.xiangban.yanglaodajie_admin.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/6/18 9:27
 */
@Service
public class OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private GoodsMapper goodsMapper;

    public Object oneWeekOrdersPrice() {
        if (commonUtils.getShopId(request) == null){
            throw new BizException(ApiResultCode.FLAG_FAIL_VOUCHER);
        }
            List<String> oneWeek = DateUtil.getOneWeek();
            OrdersDto ordersDto = new OrdersDto();
            ordersDto.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
            ordersDto.setValue2(oneWeek);
            List<OrdersDto> ordersDtos = ordersMapper.selectOneWeekOrdesPrice(ordersDto);
            ArrayList<String> list = new ArrayList<>();
            OrdersData ordersData = new OrdersData();
            for (String s :oneWeek) {
                boolean bool = false;
                for (int m = 0; m < ordersDtos.size(); m++) {
                    if (s.equals(ordersDtos.get(m).getDate1())){
                        list.add(ordersDtos.get(m).getGoodsPrice());
                        bool = true;
                        break;
                    }
                }
                if (!bool){
                    list.add("0");
                    ordersData.setOneWeekPrice(list);
                }
            }
            return ordersData;
    }

    public Object homePage() {
        if (commonUtils.getShopId(request) == null){
            throw new BizException(ApiResultCode.FLAG_FAIL_VOUCHER);
        }
        HashMap<String, Object> map = new HashMap<>();
        HashMap<String, Object> maps = new HashMap<>();
        String shopId = commonUtils.getShopId(request);
        map.put("shopId",shopId);
        Orders orders = ordersMapper.selectOrders(map);
        map.put("today",new SimpleDateFormat("yyyy-MM-dd").format( new Date()));
        Orders orders1 = ordersMapper.selectOrders(map);
        Integer integer = goodsMapper.selectGoodsNumber(map);
        if (orders == null){//总订单量
            orders.setNumber(0);
            maps.put("AllOrders",orders.getNumber());
        }else {
            maps.put("AllOrders",orders.getNumber());
        }
        if (orders1.getNumber() == 0){//今日订单和今日销售额

            maps.put("todayOrders",orders1.getNumber());
            maps.put("todaySale",0);
        }else {
            maps.put("todayOrders",orders1.getNumber());
            maps.put("todaySale",orders1.getGoodsPrice());
        }
        maps.put("GoodsForSale",integer);//在售商品
        return maps;
    }
}
